/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Dennis
 */
public class MovieLibraryAdmin extends JFrame {

    private JLabel movieInfo_movieNameLabel;
    private JLabel movieInfo_movieLengthLabel;
    private JLabel movieInfo_movieYearLabel;
    
    public MovieLibraryAdmin() {
        InitUI();
    }
    
    private void InitUI() {
        
        movieInfo_movieNameLabel = new JLabel();
        movieInfo_movieLengthLabel = new JLabel();
        movieInfo_movieYearLabel = new JLabel();
        
        movieInfo_movieLengthLabel.setText("164 min");
        movieInfo_movieYearLabel.setText("1975");
        
        setJMenuBar(InitMenuBar());
        
        add(InitFileList(), BorderLayout.CENTER); 
        
        add(InitMovieInfoPanel(), BorderLayout.EAST);
        
        add(InitStatusBar(), BorderLayout.SOUTH);

        setTitle("MovieLibraryAdmin");
        setSize(1500, 1000);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    private JMenuBar InitMenuBar() {
        /*
         * Add MenuItems
         */
        JMenuBar menubar = new JMenuBar();
        ImageIcon iconNew = new ImageIcon(getClass().getResource("new.png"));
        ImageIcon iconOpen = new ImageIcon(getClass().getResource("open.png"));
        ImageIcon iconSave = new ImageIcon(getClass().getResource("save.png"));
        ImageIcon iconExit = new ImageIcon(getClass().getResource("exit.png"));
        
        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F); 
        
        JMenu add = new JMenu("Add");
        add.setMnemonic(KeyEvent.VK_A); 
        
        JMenu update = new JMenu("Update");
        update.setMnemonic(KeyEvent.VK_U);

        

        /*
         * File - Settings
         */
        JMenuItem fileSettings = new JMenuItem("Settings", iconNew);
        fileSettings.setMnemonic(KeyEvent.VK_T);
        fileSettings.setToolTipText("Program settings");
        fileSettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //TODO Code here
            }
        });
        
        /*
         * File - Exit
         */
        JMenuItem fileExit = new JMenuItem("Exit", iconExit);
        fileExit.setMnemonic(KeyEvent.VK_E);
        fileExit.setToolTipText("Exit application");
        fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
        fileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });

        /*
         * Add- Add Episode
         */
        JMenuItem addEpisode = new JMenuItem("Add Episode", iconSave);
        addEpisode.setMnemonic(KeyEvent.VK_E);
        addEpisode.setToolTipText("Add a episode");
        addEpisode.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Add episode");
                //TODO Code here
            }

        });

        /*
         * Add - Add Season
         */
        JMenuItem addSeason = new JMenuItem("Add Seaseon", iconSave);
        addSeason.setMnemonic(KeyEvent.VK_S);
        addSeason.setToolTipText("Add a complete season");
        addSeason.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //TODO Code here
            }

        });
        
        /*
         * Add - Add Series
         */
        JMenuItem addSeries = new JMenuItem("Add Series", iconSave);
        addSeries.setMnemonic(KeyEvent.VK_I);
        addSeries.setToolTipText("Add a complete serie, including all seasons and episodes");
        addSeries.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //TODO Code here
            }

        });
        
        /*
         * Update - Update
         */
        JMenuItem updateCurrent = new JMenuItem("Update", iconSave);
        updateCurrent.setMnemonic(KeyEvent.VK_U);
        updateCurrent.setToolTipText("Update selected row");
        updateCurrent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //TODO Code here
            }
        });
        
        /*
         * Update - Update all
         */
        JMenuItem updateAll = new JMenuItem("Update all", iconSave);
        updateAll.setMnemonic(KeyEvent.VK_C);
        updateAll.setToolTipText("Update complete table");
        updateAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //TODO Code here
            }
        });

        /*
         * Add to menubar
         */
        file.add(fileSettings);
        file.addSeparator();
        file.add(fileExit);
        add.add(addEpisode);
        add.add(addSeason);
        add.add(addSeries);
        update.add(updateCurrent);
        update.add(updateAll);
        menubar.add(file);
        menubar.add(add);
        menubar.add(update);
        
        return menubar;
    }
    
    private JLabel InitStatusBar() {
        JLabel statusbar = new JLabel(" Statusbar");
        statusbar.setPreferredSize(new Dimension(-1, 22));
        statusbar.setBorder(LineBorder.createGrayLineBorder());
        return statusbar;
    }
    
    private JScrollPane InitFileList() {
        File folder = new File("\\\\DISKSTATION\\Disk1\\Film\\");
        
        String[] listOfFiles = folder.list();
        
        final MovieInfo[] mi = new MovieInfo[listOfFiles.length];
        
        for (int i = 0; i < listOfFiles.length; i++) {
            mi[i] = new MovieInfo(listOfFiles[i]);
        }
        
        
        final JList sampleJList = new JList(mi);
        sampleJList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    //System.out.println(sampleJList.getSelectedValue());
                    //movieInfo_movieNameLabel.setText(mi[sampleJList.getSelectedIndex()].toString());
                    //movieInfo_movieYearLabel.setText(mi[sampleJList.getSelectedIndex()].getYear());
                    //movieInfo_movieLengthLabel.setText(mi[sampleJList.getSelectedIndex()].getLength());
                    ProcessBuilder pb = new ProcessBuilder("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe", "-f","\\\\DISKSTATION\\Disk1\\Film\\" + mi[sampleJList.getSelectedIndex()].toString());
                    try {
                        pb.start();
                    } catch (IOException ex) {
                        Logger.getLogger(MovieLibraryAdmin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        JScrollPane listPane = new JScrollPane(sampleJList);
        return listPane;
    }
    
    private JPanel InitMovieInfoPanel() {
        JPanel moviePanel = new JPanel(new GridLayout(3,1));

        moviePanel.add(movieInfo_movieNameLabel);
        moviePanel.add(movieInfo_movieLengthLabel);
        moviePanel.add(movieInfo_movieYearLabel);
        
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
        p.add(moviePanel);
        p.setPreferredSize(new Dimension(300,0));
        return p;
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MovieLibraryAdmin tvLib = new MovieLibraryAdmin();
                tvLib.setVisible(true);
            }
        });
    }
}
